package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.ModifyPasswordBody;
import com.ruoyi.common.core.domain.model.PhoneNumberBody;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.service.ISysConfigService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 注册验证
 *
 * @author ruoyi
 */
@RestController
public class SysRegisterController extends BaseController {
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private PlatformService sysPlatformService;

    @PostMapping("/register")
    public synchronized AjaxResult register(@RequestBody RegisterBody user) {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) {
            return error("当前系统没有开启注册功能！");
        }
        String msg = null;
        if (user.isSeller()) {
            return error("该功能已禁用");
            //msg = registerService.registerFranchiser(user);
        } else {
            msg = registerService.register(user);
        }

        boolean success = StringUtils.isEmpty(msg);
        return success ? success() : error(msg);
    }

    @PostMapping("/sellerPhoneCode")
    public synchronized AjaxResult sellerPhoneCode(@RequestBody PhoneNumberBody phoneNumberBody) {
        String key = "sa_phone_code:" + phoneNumberBody.getPhoneNumber();
        String code = "" + RandomUtils.nextInt(100000, 999999);
//        String code = "666888";
        logger.info("/sellerPhoneCode, phoneNumber: " + phoneNumberBody.getPhoneNumber() + ", code: " + code);
        final int expiredMinute = 5;
        redisCache.setCacheObject(key, code, expiredMinute * 60, TimeUnit.SECONDS);
        boolean ok = sysPlatformService.sendCodeMsg(phoneNumberBody.getPhoneNumber(), code, expiredMinute, PlatformService.LANGUAGE_1);
        if (!ok) {
            return error("Enter the phone number / WhatsApp number.");
        }
        return AjaxResult.success();
    }

    @PostMapping("/modify_passport_seller")
    public synchronized AjaxResult modifyPassportSeller(@RequestBody ModifyPasswordBody body) {
        String username = body.getPhoneNumber();
        String password = body.getPassword();
        SysUser sysUser = new SysUser();
        sysUser.setUserName(username);
        // 验证码开关
//        boolean captchaEnabled = configService.selectCaptchaEnabled();
//        if (captchaEnabled) {
//            validateCaptcha(username, registerBody.getCode(), registerBody.getUuid());
//        }
//        if (StringUtils.isEmpty(username)) {
//            return "用户名不能为空";
//        }
//        if (StringUtils.isEmpty(password)) {
//            return "用户密码不能为空";
//        }
//        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
//                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
//            return "账户长度必须在2到20个字符之间";
//        }
//        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
//                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
//            return "密码长度必须在5到20个字符之间";
//        }
//        if (!userService.checkUserNameUnique(sysUser)) {
//            return "保存用户'" + username + "'失败，注册账号已存在";
//        }
//
//        sysUser.setNickName(username);
//        sysUser.setPassword(SecurityUtils.encryptPassword(password));
//
//        boolean regFlag = userService.registerUser(sysUser);
//        if (!regFlag) {
//            return "注册失败,请联系系统管理人员";
//        }
//        // 设置权限
//        userService.checkUserDataScope(sysUser.getUserId());
//        userService.insertUserAuth(sysUser.getUserId(), Constants.getRole(Constants.ROLE_SELLER));
//
//        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success")));
//        return "";
        return AjaxResult.success();
    }
}
